<?php
//配置您申请的appkey
//************1.航班可售舱位及价格查询************
$url = "http://op.juhe.cn/flight/sav";
$params = array(
      "key" => "1a4449ce1d9303441fcc6771f5e46bf7",//应用APPKEY(应用详细页查询)
      "dtype" => "json",//返回数据的格式,xml或json，默认json
      "orgCity" => "CTU",//始发城市的三字码
      "dstCity" => "PVG",//到达城市的三字码
      "flightDate" => "2017-8-24",//日期（年月日） 
      "airline" => "ALL",//航空公司的两字码或者"ALL"或者""
      "direct" => true,//直航/转飞
      "eticket" => true//是否必须是电子票
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************2.航班经停地及起降时间查询************
/*$url = "http://op.juhe.cn/flight/ff";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
      "dtype" => "",//返回数据的格式,xml或json，默认json
      "flightNo" => "",//航班编号
      "flightDate" => "",//日期（年月日）
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************3.机票号对应的信息查询************
$url = "http://op.juhe.cn/flight/detr";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
      "dtype" => "",//返回数据的格式,xml或json，默认json
      "ticketNo" => "",//机票号（XXX-XXXXXXXX XXXXXXXXXXX均可）
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************4.航班时刻表查询************
$url = "http://op.juhe.cn/flight/sk";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
      "dtype" => "",//返回数据的格式,xml或json，默认json
      "orgCity" => "",//始发城市的三字码
      "dstCity" => "",//到达城市的三字码
      "flightDate" => "",//日期（年月日）
      "airline" => "",//航空公司的两字码或者"ALL"或者""
      "direct" => "",//直航/转飞
      "nostop" => "",//是否经停
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************5.票价查询************
$url = "http://op.juhe.cn/flight/fd";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
      "dtype" => "",//返回数据的格式,xml或json，默认json
      "orgCity" => "",//始发城市的三字码
      "dstCity" => "",//到达城市的三字码
      "flightDate" => "",//日期（年月日）
      "airline" => "",//航空公司的两字码或者"ALL"或者""
      "planeModel" => "",//机型(未找到机型的以干线机型处理)
      "passType" => "",//旅客类型("AD"：成人；"IN"：婴儿；"CH"：儿童)
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************6.特价票查询************
$url = "http://op.juhe.cn/flight/nfd";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
      "dtype" => "",//返回数据的格式,xml或json，默认json
      "orgCity" => "",//始发城市的三字码
      "dstCity" => "",//到达城市的三字码
      "flightDate" => "",//日期（年月日）
      "airline" => "",//航空公司的两字码或者"ALL"或者""
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************7.机场/城天气预报************
$url = "http://op.juhe.cn/flight/wf";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
      "dtype" => "",//返回数据的格式,xml或json，默认json
      "cityCode" => "",//该城市的三字代码
      " " => "",//
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************8.航空公司规定退改签查询************
$url = "http://op.juhe.cn/flight/nfn";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
      "dtype" => "",//返回数据的格式,xml或json，默认json
      "orgCity" => "",//始发城市的三字码
      "dstCity" => "",//到达城市的三字码
      "flightDate" => "",//日期（年月日）
      "airline" => "",//航空公司的两字码或者"ALL"或者""
      "cabin" => "",//舱位
      " " => "",//
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 */
 
 
 
 
/**
 * 请求接口返回内容
 * @param  string $url [请求的URL地址]
 * @param  string $params [请求的参数]
 * @param  int $ipost [是否采用POST形式]
 * @return  string
 */
function juhecurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();
 
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}
?>